package com.ice.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.ice.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ice.common.IceConstants;
import com.ice.common.IceLog;
import com.ice.dao.UserDao;
import com.ice.service.UserService;

@Service("UserService")
public class UserServiceImpl implements UserService{

	Logger  log = LoggerFactory.getLogger(UserServiceImpl.class);
	@Resource
	private UserDao userDao;
	
	@Override
	public boolean enroll(Map<String, Object> enrollMap) {
		// TODO Auto-generated method stub
		try {
			userDao.save(enrollMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public User findByOpenid(int activeid,String openid) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("activeid", activeid);
		map.put("openid", openid);
		return userDao.findOne(map);
	}

	@Override
	public  List<User> findUserNotAudit(int activeid) {
		// TODO Auto-generated method stub
		int isaudit = IceConstants.WECHAT_USER_NOT_AUDIT;
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("activeid", activeid);
		map.put("audit", isaudit);
		return userDao.findAll(map);
	}

	@Override
	public  List<User> findUsrIsAudit(int activeid) {
		// TODO Auto-generated method stub
		int isaudit = IceConstants.WECHAT_USER_IS_AUDIT;
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("activeid", activeid);
		map.put("audit", isaudit);
		return userDao.findAll(map);
	}

	@Override
	public  List<User> findUserFailAudit(int activeid) {
		// TODO Auto-generated method stub
		int isaudit = IceConstants.WECHAT_USER_FAIL_AUDIT;
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("audit", isaudit);
		map.put("activeid", activeid);
		return userDao.findAll(map);
	}

	@Override
	public List<User> findAllActiveByOpenid(int openid) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("openid", openid);
		return  userDao.findAll(map);
	}

	@Override
	public boolean accessAudit(int activeid, String openid) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("audit",IceConstants.WECHAT_USER_IS_AUDIT);
		map.put("activeid", activeid);
		map.put("openid", openid);
		try {
			userDao.update(map);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean refuseAudit(int activeid, String openid) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("audit",IceConstants.WECHAT_USER_FAIL_AUDIT);
		map.put("activeid", activeid);
		map.put("openid", openid);
		try {
			userDao.update(map);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}


	

}
